package gui;

import engine.Factory;

import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * A collection of Buttons to perform various tasks for our GUI.
 * 
 * @author jianweigan
 */
@SuppressWarnings("serial")
public class ButtonPanel extends JPanel {

	private Canvas myDisplay;
	private SettingsPanel mySettings;

	/**
	 * Creates a button panel that allows the user to add FactoryButtons.
	 * Standard buttons created are also created (Clear and Remove Buttons)
	 * 
	 * @param target
	 *            Canvas to link buttons in ButtonPanel to
	 * @param settings
	 *            SettingsPanel to associated ButtonPanel to
	 */
	public ButtonPanel(Canvas target, SettingsPanel settings) {
		myDisplay = target;
		mySettings = settings;
		addClearButton();
		addRemoveButton();
	}

	/**
	 * Create a button for the given command.
	 * 
	 * @param factory
	 *            command to activate when button is pushed
	 */
	public void addFactoryButton(Factory factory) {
		FactoryButton b = new FactoryButton(factory);
		add(b);
	}

	/**
	 * Creates a button that removes currently selected group.
	 */
	public void addRemoveButton() {
		JButton remove = new JButton(ResourceStrings.getString("RemoveButton"));
		remove.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mySettings.removeCurrentGroup();
			}
		});
		add(remove);
	}

	/**
	 * Creates the clear viewer button and adds it to the GUI. This button
	 * removes all groups from the Canvas.
	 */
	private void addClearButton() {
		JButton clear = new JButton(ResourceStrings.getString("ClearButton"));
		clear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mySettings.clearGroups();
				myDisplay.clear();
				myDisplay.repaint();
			}
		});
		add(clear);
	}

	/**
	 * This class implements a button that creates Groups associated with the
	 * type of Factory it is constructed with.
	 * 
	 * Created as an inner class to be able to access myDisplay and mySettings
	 * instance variables, so they need not be instantiated within this class as
	 * well.
	 * 
	 * @author jianweigan
	 * 
	 */
	public class FactoryButton extends JButton implements ActionListener {

		private Factory myFactory;

		/**
		 * Creates a button linked to the factory provided
		 * 
		 * @param factory
		 */
		public FactoryButton(Factory factory) {
			super(factory.getName());
			myFactory = factory;
			addActionListener(this);
		}

		/**
		 * Called when button clicked. Creates a group of type associated with
		 * the factory of this button. Adds the newly added group to the group
		 * chooser
		 */
		public void actionPerformed(ActionEvent e) {
			myFactory.createGroup(myDisplay);
			mySettings.addChoosable(myFactory.getNewestGroup());
		}

		/**
		 * For SettingsPanel to access the factory to obtain Behavior settings
		 * from the Group
		 */
		public Factory getFactory() {
			return myFactory;
		}
	}
}
